﻿using DataType.Actions;
using DataType.CustomEnumType;
using DataType.JudgeResults;
using MapCalclator;
using OprsSimulator.Operators;
using OprsSimulator.OprAttributes;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ActionsJudge
{
    internal static class CancelJMPlanJudge
    {
        public static List<IJudgeResult> ToJudge(IBasicAction acts, List<IOperator> oprs, Map map)
        {
            //judgeResults保存裁决结果，用于最后的返回
            List<IJudgeResult> judgeResults = new List<IJudgeResult>();
            //取得并判断数据
            CancelJMPlanAction action = (CancelJMPlanAction)acts;
            IOperator opr = null;// 取得算子变量
            foreach (IOperator oper in oprs)
            {
                if (oper.ObjID == action.ObjID)
                {
                    opr = oper;
                    break;
                }
            }
            if (opr == null)
            {
                Trace.TraceWarning($"无效裁决：态势中没有ID为{action.ObjID}的算子");// 输出日志
                return new List<IJudgeResult>();
            }
            if (action.Type != ActionType.CancelJMPlan)
            {
                Trace.TraceWarning("无效裁决：算子的动作类型有误，应该是取消间瞄计划类型");
                return new List<IJudgeResult>();
            }
            // 没有继承接口
            if (!typeof(ICommonAttris).IsAssignableFrom(opr.GetType()) ||
                !typeof(IAttackAttris).IsAssignableFrom(opr.GetType()))
            {
                Trace.TraceWarning($"无效裁决：算子{action.ObjID}不具有取消间瞄计划所需要的全部属性");
                return new List<IJudgeResult>();
            }
            // 判断
            OprType type = (OprType)opr.GetType().GetProperty("Type").GetValue(opr);
            OprSubType sub_type = (OprSubType)opr.GetType().GetProperty("Subtype").GetValue(opr);
            int weapon_cool_time = (int)opr.GetType().GetProperty("WeaponCoolTime").GetValue(opr);
            int change_state_remain_time = (int)opr.GetType().GetProperty("ChangeStateRemainTime").GetValue(opr);
            int weapon_unfold_time = (int)opr.GetType().GetProperty("WeaponUnfoldTime").GetValue(opr);

            bool flag_cancel_JM_plan = false;
            // 炮兵算子，间瞄前150秒 --> 300 - 已经过时间 > 150
            if (type == OprType.Vehicle && sub_type == OprSubType.Artillery)
            {
                if (weapon_cool_time >= 150 && change_state_remain_time <= 0 && weapon_unfold_time <= 0)// 间瞄前150秒，且没有在转换状态，没有在展开或者收起武器
                {
                    flag_cancel_JM_plan = true;
                }
            }

            // 生成裁决结果，添加进judgeResults
            CancelJMPlanJudgeResult JMPlanJudgeResult = new CancelJMPlanJudgeResult(action.ObjID, flag_cancel_JM_plan);
            judgeResults.Add(JMPlanJudgeResult);

            return judgeResults;
        }
    }
}
